package com.cssw.fyzb.core.eao.video;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.core.es.EsClient;
import com.cssw.fyzb.core.model.eos.NotifyEo;
import com.cssw.fyzb.core.util.DateUtil;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Jian Qin.
 */
@Component
public class NotifyEaoImpl implements NotifyEao{
    @Autowired
    private EsClient esClient;

    private final static String INDEX_NAME = "fyzb_im_notify";
    private final static String TYPE_NAME = "im_notify";

    @Override
    public void save(Long userId, Long toUserId, String cmd, String tps, String content,
                     Long videoId, String thumbS, Date date) {
        NotifyEo notifyEo = new NotifyEo();
        notifyEo.setUserId(userId);
        notifyEo.setToUserId(toUserId);
        notifyEo.setCmd(cmd);
        notifyEo.setTips(tps);
        notifyEo.setThumbs(thumbS);
        notifyEo.setCreateTime(DateUtil.format(date));
        notifyEo.setVideoId(videoId);
        notifyEo.setContent(content);


        esClient.getClient()
                .prepareIndex(INDEX_NAME, TYPE_NAME)
                .setSource(JSON.parseObject(JSON.toJSONString(notifyEo)))
                .get()
        ;

    }

    @Override
    public Page<NotifyEo> pageAtMeByUserId(Long userId, Integer pageNumber, Integer pageSize) {
        pageNumber = pageNumber - 1;

        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        queryBuilder.filter().add(QueryBuilders.termQuery("toUserId", userId));
        queryBuilder.filter().add(QueryBuilders.termQuery("cmd", "at"));

        SearchResponse response = esClient.getClient()
                .prepareSearch(INDEX_NAME)
                .setTypes(TYPE_NAME)
                .setQuery(queryBuilder)
                .setFrom(pageNumber * (pageSize - 1))
                .setSize(pageSize)
                .addSort(
                        SortBuilders.fieldSort("createTime").order(SortOrder.DESC)
                ).get();

        return toPage(response, pageNumber, pageSize);
    }

    @Override
    public Page<NotifyEo> pageCommentByUserId(Long userId, Integer pageNumber, Integer pageSize) {
        pageNumber = pageNumber - 1;

        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        queryBuilder.filter().add(QueryBuilders.termQuery("toUserId", userId));
        queryBuilder.filter().add(QueryBuilders.termQuery("cmd", "comment"));

        SearchResponse response = esClient.getClient()
                .prepareSearch(INDEX_NAME)
                .setTypes(TYPE_NAME)
                .setQuery(queryBuilder)
                .setFrom(pageNumber * (pageSize - 1))
                .setSize(pageSize)
                .addSort(
                        SortBuilders.fieldSort("createTime").order(SortOrder.DESC)
                ).get();

        return toPage(response, pageNumber, pageSize);
    }

    @Override
    public Page<NotifyEo> pageLikeByUserId(Long userId, Integer pageNumber, Integer pageSize) {
        pageNumber = pageNumber - 1;

        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        queryBuilder.filter().add(QueryBuilders.termQuery("toUserId", userId));
        queryBuilder.filter().add(QueryBuilders.termQuery("cmd", "like"));

        SearchResponse response = esClient.getClient()
                .prepareSearch(INDEX_NAME)
                .setTypes(TYPE_NAME)
                .setQuery(queryBuilder)
                .setFrom(pageNumber * (pageSize - 1))
                .setSize(pageSize)
                .addSort(
                        SortBuilders.fieldSort("createTime").order(SortOrder.DESC)
                ).get();

        return toPage(response, pageNumber, pageSize);
    }

    @Override
    public Page<NotifyEo> pageFocusByUserId(Long userId, Integer pageNumber, Integer pageSize) {
        pageNumber = pageNumber - 1;

        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        queryBuilder.filter().add(QueryBuilders.termQuery("toUserId", userId));
        queryBuilder.filter().add(QueryBuilders.termQuery("cmd", "focus"));

        SearchResponse response = esClient.getClient()
                .prepareSearch(INDEX_NAME)
                .setTypes(TYPE_NAME)
                .setQuery(queryBuilder)
                .setFrom(pageNumber * (pageSize - 1))
                .setSize(pageSize)
                .addSort(
                        SortBuilders.fieldSort("createTime").order(SortOrder.DESC)
                ).get();

        return toPage(response, pageNumber, pageSize);
    }

    private Page<NotifyEo> toPage(SearchResponse response, int pageNumber, int pageSize) {
        Page<NotifyEo> page = new Page<>();
        int count = (int) response.getHits().totalHits;
        page.setTotalPage((int) Math.ceil(count * 1.0 / pageSize));
        page.setTotalRow(count);
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        List<NotifyEo> list = new ArrayList<>();
        for (SearchHit hit : response.getHits()) {
            list.add(JSON.parseObject(JSON.toJSONString(hit.getSourceAsMap()), NotifyEo.class));
        }
        page.setList(list);
        return page;
    }
}
